package com.hm.shopping.web.buyer;

import java.net.URLEncoder;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hm.base.def.UserSexEnum;
import com.hm.base.service.SubscriberService;
import com.hm.base.vo.SubscriberVo;
import com.hm.common.util.StringUtil;
import com.hm.shopping.auto.su.BaseController;
import com.hm.shopping.auto.su.R.Uri;
import com.hm.shopping.auto.su.R.Uri.Shopping;
import com.hm.shopping.auto.su.R.Uri.Shopping.BuyerPersonalCenter;
import com.hm.shopping.auto.su.helper.HmSessionFactory;
import com.hm.shopping.service.FavoritesService;
import com.hm.shopping.service.ShopcartService;
import com.hm.shopping.service.UserService;
import com.hm.shopping.su.BeanUtil;
import com.hm.shopping.vo.ShoppingSubscriberVo;
import com.hm.shopping.vo.su.UserAddressVo;

/**
 * @author shishun.wang
 * @date 2018年1月26日 下午5:27:12
 * @version 1.0
 * @describe 买家中心
 */
@Controller
@RequestMapping(Uri.PC + "/buyer")
public class BuyerController extends BaseController {

	@Autowired
	private UserService userService;

	@Autowired
	private ShopcartService shopcartService;

	@Autowired
	private FavoritesService favoritesService;
	
	@Autowired
	private SubscriberService subscriberService;
	

	/**
	 * 修改买家基本信息
	 * 
	 * @param subscriberVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "updateUserInfo.do", method = RequestMethod.POST)
	public String updateUserInfo(ShoppingSubscriberVo subscriberVo) throws Exception {
		try {
			SubscriberVo subscriber = new SubscriberVo();
			BeanUtil.copyProperties(subscriberVo, subscriber);
			subscriber.setSex(UserSexEnum.MAN);
			subscriberService.updateSubscriber(HmSessionFactory.currentUserId(), subscriber);

			super.logout();
			return "redirect:" + "/no_data_access.do?m=" + URLEncoder.encode("账户信息发生变更请重新登陆", "utf-8");
		} catch (Exception e) {
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_INFO + "?errorMsg=" + URLEncoder.encode(e.getMessage(), "utf-8");
		}
	}

	/**
	 * 修改买家收货地址
	 * 
	 * @param addressVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "updateUserAddress.do", method = RequestMethod.POST)
	public String updateUserAddress(UserAddressVo addressVo) throws Exception {
		try {
			String msg = StringUtil.isBlankOrNull(addressVo.getAddressId()) ? "添加用户收货地址成功" : "修改用户收货地址成功";
			userService.updateUserAddress(HmSessionFactory.currentUserId(), addressVo);
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_ADDRESS + "?successfulMsg=" + URLEncoder.encode(msg, "utf-8");
		} catch (Exception e) {
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_ADDRESS + "?errorMsg=" + URLEncoder.encode(e.getMessage(), "utf-8");
		}
	}

	/**
	 * 设置买家默认收获地址
	 * 
	 * @param addressId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "settingUserDefaultAddress.do", method = RequestMethod.GET)
	public String settingUserDefaultAddress(String addressId) throws Exception {
		try {
			userService.settingUserDefaultAddress(HmSessionFactory.currentUserId(), addressId);
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_ADDRESS + "?successfulMsg=" + URLEncoder.encode("设置用户默认收货地址成功", "utf-8");
		} catch (Exception e) {
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_ADDRESS + "?errorMsg=" + URLEncoder.encode(e.getMessage(), "utf-8");
		}
	}

	/**
	 * 删除买家收货地址
	 * 
	 * @param addressId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteUserAddress.do", method = RequestMethod.GET)
	public String deleteUserAddress(String addressId) throws Exception {
		try {
			userService.deleteUserAddress(HmSessionFactory.currentUserId(), addressId);
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_ADDRESS + "?successfulMsg=" + URLEncoder.encode("删除用户地址成功", "utf-8");
		} catch (Exception e) {
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_ADDRESS + "?errorMsg=" + URLEncoder.encode(e.getMessage(), "utf-8");
		}
	}

	/**
	 * 加入购物车
	 * 
	 * @param goodsId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "joinShopcart.do", method = RequestMethod.GET)
	public String joinShopcart(String goodsId, String[] goodsAttributes, Integer quantity) throws Exception {
		try {
			shopcartService.joinShopcart(goodsId, HmSessionFactory.currentUserIdString(), Arrays.asList(goodsAttributes), quantity);
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_SHOPCART + "?successfulMsg=" + URLEncoder.encode("加入购物车成功", "utf-8");
		} catch (Exception e) {
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_SHOPCART + "?errorMsg=" + URLEncoder.encode(e.getMessage(), "utf-8");
		}
	}

	/**
	 * 购物车商品数量修改
	 * 
	 * @param goodsId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "addShopcart.do", method = RequestMethod.GET)
	public String addShopcart(String shopcartId, int count) throws Exception {
		try {
			shopcartService.addShopcart(shopcartId, count);
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_SHOPCART;
		} catch (Exception e) {
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_SHOPCART;
		}
	}

	/**
	 * 修改购物车商品是否勾选中
	 * 
	 * @param goodsId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "updateShopcartSelected.do", method = RequestMethod.GET)
	public String updateShopcartSelected(String shopcartId) throws Exception {
		try {
			shopcartService.updateShopcartSelected(shopcartId);
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_SHOPCART;
		} catch (Exception e) {
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_SHOPCART;
		}
	}

	/**
	 * 移除购物车
	 * 
	 * @param goodsId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "removeShopcart.do", method = RequestMethod.GET)
	public String removeShopcart(String shopcartId) throws Exception {
		try {
			shopcartService.removeShopcart(shopcartId);
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_SHOPCART + "?successfulMsg=" + URLEncoder.encode("移除购物车成功", "utf-8");
		} catch (Exception e) {
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_SHOPCART + "?errorMsg=" + URLEncoder.encode(e.getMessage(), "utf-8");
		}
	}

	/**
	 * 添加收藏
	 * 
	 * @param goodsId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "addFavorites.do", method = RequestMethod.GET)
	public String addFavorites(String goodsId) throws Exception {
		try {
			favoritesService.addFavorites(HmSessionFactory.currentUserIdString(), goodsId);
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_FAVORITES + "?successfulMsg=" + URLEncoder.encode("添加收藏成功", "utf-8");
		} catch (Exception e) {
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_FAVORITES + "?errorMsg=" + URLEncoder.encode(e.getMessage(), "utf-8");
		}
	}
	
	/**
	 * 删除收藏
	 * 
	 * @param goodsId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "delFavorites.do", method = RequestMethod.GET)
	public String delFavorites(String favoriteId) throws Exception {
		try {
			favoritesService.delFavorites(favoriteId);
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_FAVORITES + "?successfulMsg=" + URLEncoder.encode("删除收藏成功", "utf-8");
		} catch (Exception e) {
			return "redirect:" + Shopping.BUYER_PERSONAL_CENTER + BuyerPersonalCenter.PERSONAL_FAVORITES + "?errorMsg=" + URLEncoder.encode(e.getMessage(), "utf-8");
		}
	}
}
